package com.g8.pethospitallearnbackend.controller;

import com.g8.pethospitallearnbackend.entity.Disease;
import com.g8.pethospitallearnbackend.entity.Learnprocess;
import com.g8.pethospitallearnbackend.exception.CustomException;
import com.g8.pethospitallearnbackend.service.LearnProcessService;
import com.g8.pethospitallearnbackend.utils.responseUtil.ResponseStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/case-learn")
public class LearnProcessController {
    @Autowired
    private LearnProcessService learnProcessService;

    /**
     * 返回所有学习流程对应的模块，方便从病例中取到加入每个流程中
     * @return
     */
    @GetMapping("")
    public List<Learnprocess> getAllProcesses(){
        List<Learnprocess> learnprocesses=new ArrayList<>();
        learnprocesses=learnProcessService.getAllLearnProcesses();
        if(learnprocesses==null||learnprocesses.isEmpty()){
            throw new CustomException(ResponseStatusEnum.DATA_NOT_EXIST,"没有学习流程");
        }
        return learnprocesses;
    }

    /**
     * 根据病例id查询涉及的病的信息
     * @param caseId
     * @return
     */
    @GetMapping("/case-diseaseinfo/{caseId}")
    public List<Disease> getRelevantDiseasesFromCase(@PathVariable String caseId){
        List<Disease> diseases=new ArrayList<>();
        diseases=learnProcessService.getRelevantDiseasesFromCase(caseId);
        if(diseases==null|| diseases.isEmpty()) throw new CustomException(ResponseStatusEnum.DATA_NOT_EXIST,caseId+"没有相关病名信息");
        return diseases;
    }


    /**
     * 添加一个新流程
     * @param learnprocess
     * @return
     */
    @PostMapping("/process")
    public String addANewProcess(@RequestBody Learnprocess learnprocess){
        String newId=learnProcessService.addANewProcess(learnprocess);
        if(newId==null) throw new CustomException(ResponseStatusEnum.SYSTEM_ERROR,"添加失败");
        return newId;
    }

    /**
     * 删除一个流程，要修改order
     * @param proId
     * @return
     */
    @DeleteMapping("/process/{proId}")
    public String deleteAProcess(@PathVariable String proId){
        String deletedId=learnProcessService.deleteAProcess(proId);
        if(deletedId==null) throw new CustomException(ResponseStatusEnum.BAD_REQUEST,"删除"+proId+"失败");
        return deletedId;
    }

    @PutMapping("/process/{proId}")
    public String updateAProcess(@PathVariable String proId, @RequestBody Learnprocess learnprocess){
        String updatedProcess=learnProcessService.updateAProcess(proId,learnprocess);
        return updatedProcess;
    }
}
